package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.CarrierBusinessEntity;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author : 熊丽
 * @date :2021/4/13 0013$ 17:06$
 * @Description ://货车承运model$
 */
public class FreightCarTransportationModel extends AndroidViewModel {
    public MutableLiveData<CarrierBusinessEntity> mCarrierBusinessEntityMutableLiveData = new MutableLiveData<>();

    public FreightCarTransportationModel(@NonNull Application application) {
        super(application);
    }

    public void getList(Context context, int page) {
        Map<String, Object> map = new HashMap<>();
        DialogHelper.showProgressDialog(context, "加载中", 0, false);

        if (UserOperating.getInstance().isCompany()) {
            map.put("companyNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
            map.put("teamNo", "");
        } else if (UserOperating.getInstance().isTeam()){
            map.put("companyNo", "");
            map.put("teamNo", UserOperating.getInstance().getTeamOrCompanyNameNo());
        }else {
            map.put("terminal", 3);
        }
        map.put("page", page);
        map.put("limit",10);
        NewAPIManager.getInstance().getLogisticsAPI().getoperateCarrierBusinessList(map).enqueue(new Callback<BaseEntity<CarrierBusinessEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<CarrierBusinessEntity>> call, @NotNull Response<BaseEntity<CarrierBusinessEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null || response.body().getData().getPage() == null)
                    return;
                mCarrierBusinessEntityMutableLiveData.postValue(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<CarrierBusinessEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }

}
